package com.springmvc.demo.controller;

import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.springmvc.demo.model.User;
import com.springmvc.demo.service.UserService;

@Controller
public class LoginController {
	@Autowired
	UserService userServiceImpl;
	
	@RequestMapping(value="/getRole")
	@ResponseBody
	public JSONObject getRole(Map<String, Object> map) {
		
		JSONArray str=(JSONArray)JSONArray.toJSON(userServiceImpl.getRoleList());
		JSONObject rs=new JSONObject();
		//map.addAttribute("roles", jsonObject);
		//System.out.println(str);
		map.put("roles", str);
		rs.put("rs",str);
		return rs;
	}
	
	@RequestMapping("/login")
	public String login() {
		return "login";
	}
	
	@RequestMapping("/userLogin")
	public String userLogin(User usr,Map<String,Object> map,HttpSession session) {
		System.out.println(usr.getRoleid());
		User u=userServiceImpl.login(usr);	       	       
	       if(u!=null) {
	    	   
	    	   System.out.println("============"+u.getUserid());
	    	   //map.put("name", u.getUserid());
	    	   session.setAttribute("name", u.getUserid());
	    	   session.setAttribute("roleid", u.getRoleid());
	    	   //map.put("name", u.getUserid());
	    	  
	    	   return "main"; 
	       }else {
	    	   return "error";
	       }
	}
	
}
